import requests
from lxml import etree

lis = []
lis_1 = []
for i in range(1, 10):
    url = f'https://www.kuaidaili.com/free/inha/{i}/'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
    }
    resp = requests.get(url=url, headers=headers)
    print(resp)
    et = etree.HTML(resp.text)
    tbody = et.xpath('//*[@id="list"]/table/tbody/tr')
    for tr in tbody:
        ip = tr.xpath('./td[1]/text()')[0]
        # ip = tr.xpath('//*[@id="list"]/table/tbody/tr[1]/td[1]/text()')[0]
        pros = tr.xpath('./td[2]/text()')[0]
        # print(ip, pros)

        proxy = ip + ':' + pros
        proxies_dict = {
            "http": "http://" + proxy,
            "https": "http://" + proxy,
        }
        # print(proxies_dict)
        lis.append(proxies_dict)
        try:
            response = requests.get(url=url, proxy=proxies_dict, timeout=1)  # proxy代理，timeout 延时
            if response.status_code == 200:  # status_code 状态码
                print('当前ip代理是：', proxies_dict, '可用')
                lis_1.append(proxies_dict)
        except:
            print('当前ip代理是：', proxies_dict, '请求超时，检测不合格')

print(len(lis))
print(lis_1)
